const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";

function Avocet() {
  // Index of last tab open in window
  this.lastTab = 0;
  // Array to hold all open panes
  this.panes = new Array();
}

// Open new pane and open file for editing
Avocet.prototype.openFile = function() {
  // Create a new tab for open file
  this.addPane();
  // Select new tab
  document.getElementById("tabbox").selectedIndex = avocet.lastTab;
  var openPane = this.getPane(this.getSelectedTabIndex());

  // Create open file dialog
  var nsIFilePicker = Components.interfaces.nsIFilePicker;
  var openFileDialog = Components.classes["@mozilla.org/filepicker;1"]
                        .createInstance(nsIFilePicker);
  // Show open file dialog
  openFileDialog.init(window, "Select a File", nsIFilePicker.modeOpen);
  var result = openFileDialog.show();
  if (result == nsIFilePicker.returnOK) {
    // Get path of file selected in dialog
    var filePath = openFileDialog.file.path;
    var fio = new Filio(filePath);
    // Set text box to contents of text file
    openPane.setText(fio.read());
    // Save path of file and designate as initialized
    openPane.setFilePath(filePath);
    openPane.setInitialized(true);
    // Set title of tab to file name
    var tab = this.getTab(this.getSelectedTabIndex());
    tab.setAttribute("label", openPane.getFileName());
  }
}

// Save file in active pane, show dialog if it has not been saved yet
Avocet.prototype.saveFile = function(index) {
  var savePane = this.getPane(index);
  
  // Show save dialog if file has not been initialized yet
  if(savePane.getInitialized() == false) {
    this.saveFileAs(index);  
  }
  // Set title of tab to file name
  var tab = this.getTab(index);
  tab.setAttribute("label", savePane.getFileName());
  
  // Write to file
  var fio = new Filio(savePane.getFilePath());
  // Create new file if one does not already exist
  if(fio.exists() == false) {
    fio.create();
  }
  // Save file
  fio.write(savePane.getText());
}

Avocet.prototype.saveFileAs = function(index) {
  var savePane = this.getPane(index);
  var nsIFilePicker = Components.interfaces.nsIFilePicker;
  var saveFileDialog = Components.classes["@mozilla.org/filepicker;1"]
                        .createInstance(nsIFilePicker);
  // Show save file dialog
  saveFileDialog.init(window, "Select a File", nsIFilePicker.modeSave);
  var result = saveFileDialog.show();
  if (result == nsIFilePicker.returnOK) {
    var filePath = saveFileDialog.file.path;
    // Save path of file and designate as initialized
    savePane.setFilePath(filePath);
    savePane.setInitialized(true);
  }
  this.saveFile(index);
}

// Get index of active tab (starting at 0)
Avocet.prototype.getSelectedTabIndex = function() {
  return document.getElementById("tabbox").getAttribute("selectedIndex");
}

// Get XIDEPane object for the active tab (see XIDEPane.js)
Avocet.prototype.getPane = function(index) {
  return this.panes[index];
}

// Get tab object for active tab
Avocet.prototype.getTab = function(index) {
  var query = "#tabbox > tabs > tab:eq(" + index + ")";
  return $(query).get()[0];
}

// Get textbox object for active tab
Avocet.prototype.getTextbox = function(index) {
  query = "#tabpanels > tabpanel > textbox:eq(" + index + ")";
  return $(query).get()[0];
}

// Get tabpanel object for active tab
Avocet.prototype.getPanel = function(index) {
  query = "#tabpanels > tabpanel:eq(" + index + ")";
  return $(query).get()[0];
}

// Create a new tab for editing
Avocet.prototype.addPane = function() {
  this.lastTab++;
  var newPane = new Pane();
  // Add XUL to window
  newPane.construct();
  // Add new pane object to panes array
  this.panes.push(newPane);
}

// Remove tab of given index
Avocet.prototype.removePane = function(index) {
  this.lastTab--;
  // Get pane of tab to be closed
  var removePane = this.getPane(index);
  // Remove XUL from window
  removePane.destroy();
  // Remove pane object from panes array 
  this.panes.splice(index);
}

// Change title of tab and state of pane when textbox changes
Avocet.prototype.textChange = function() {
  var activePane = this.getPane(this.getSelectedTabIndex());
  var activeTab = this.getTab(this.getSelectedTabIndex());
  activePane.setSaved(false);
  // Add asterisk to tab title of file hasn't been saved
  if(activeTab.getAttribute("label").indexOf('*') == -1) {
    var newLabel = activeTab.getAttribute("label") + "*";
    activeTab.setAttribute("label", newLabel);
  }
}
